<template>
  <page-header-wrapper :title="false">
    <a-card bordered style="margin-top: 20px">
      <a-tabs type="card" default-active-key="1">
        <a-tab-pane key="1" tab="商标意向"></a-tab-pane>
<!--        <a-tab-pane key="2" tab="专利意向" disabled></a-tab-pane>-->
      </a-tabs>
      <div class="table-page-search-wrapper">
        <a-form-model
          layout="inline"
          :model="queryParam"
        >
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="商标名称">
                <a-input v-model="queryParam['filter[tm_name]']" placeholder="请输入商标名称" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="商标类别">
                <a-select v-model="queryParam['filter[category]']" placeholder="请选择商标类别" :allowClear="true">
                  <a-select-option v-for="(item, index) in categoryOpts" :value="index" :key="index">{{item}}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="注册号">
                  <a-input v-model="queryParam['filter[tm_reg_no]']" placeholder="请输入注册号" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="进展状态">
                  <a-select v-model="queryParam['filter[status]']" placeholder="请选择进展状态">
                    <a-select-option v-for="(item, index) in statusOpts" :value="index" :key="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="发起时间">
                  <a-range-picker v-model="queryParam['created_at_start']" @change="onChange" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetForm">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          ref="table"
          bordered
          size="middle"
          :row-key="(record) => record.id"
          :columns="intentionColumns"
          :data="intentionData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterCategory"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in categoryOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterType"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in typeOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRegion"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in regionOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterStatus"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-select
              :placeholder="`选择${column.title}`"
              style="width: 188px; margin-bottom: 8px; display: block;"
              :value="selectedKeys[0]"
              @change="(value) => setSelectedKeys(value ? [value] : [])"
              @pressEnter="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              <a-select-option v-for="(item, index) in statusOpts" :value="index" :key="index">{{item}}</a-select-option>
            </a-select>
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSelect(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRange"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-range-picker
              style="width: 228px; margin-bottom: 8px; display: block;"
              :value="createValue"
              @change="onChangeDate"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 110px; margin-right: 8px"
              @click="() => handleRange(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 110px" @click="() => rangeReset(clearFilters)">
              重置
            </a-button>
          </div>
          <div
            slot="filterRange1"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-range-picker
              style="width: 228px; margin-bottom: 8px; display: block;"
              :value="createValue1"
              @change="onChangeDate1"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 110px; margin-right: 8px"
              @click="() => handleRange(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 110px" @click="() => rangeReset1(clearFilters)">
              重置
            </a-button>
          </div>
<!--          <template slot="countdown" slot-scope="text">-->
<!--            <span style="color: red">{{text}}</span>-->
<!--          </template>-->
          <template slot="tm_type" slot-scope="text">
            {{typeOpts[text]}}
          </template>
          <template slot="region" slot-scope="text">
            {{regionOpts[text]}}
          </template>
          <template slot="status" slot-scope="text,record">
            <a-button size="small" type="primary" @click="onEdit(record.id)">{{statusOpts[text]}}</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      centered
      title="进展状态编辑"
      v-model="visible"
      @ok="onSubmit"
    >
      <a-form-model
        ref="form"
        :model="statusForm"
      >
        <!--        预定按钮-->
        <a-form-model-item label="进展状态">
          <a-radio-group v-model="statusForm.status">
            <a-radio v-for="(item,index) in itemOpts" :key="index" :value="index">{{item}}</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!--        <a-form-model-item v-if="form.status == 2" prop="notes" label="失败备注">-->
        <!--          <a-input type="textarea" placeholder="注：交易失败请填写原因"/>-->
        <!--        </a-form-model-item>-->
        <a-form-model-item label="备注">
          <a-textarea :rows="5" v-model="statusForm.notes" placeholder="注：如选择释放请填写原因"/>
        </a-form-model-item>
        <!--        释放按钮-->
        <!--        <a-form-model-item label="进展状态">-->
        <!--          <a-radio-group>-->
        <!--            <a-radio value="1">等待</a-radio>-->
        <!--          </a-radio-group>-->
        <!--        </a-form-model-item>-->
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
  import { STable, Ellipsis } from '@/components'
  import transactionColumns from '@/columns/transaction'
  import { getEditIntention, getIntentList, postEditIntention } from '@/api/transaction'

  export default {
    name: 'Intention',
    components: { STable, Ellipsis },
    data () {
      return {
        form: {},
        visible: false,
        advanced: true,
        queryParam: {},
        createValue: [],
        createValue1: [],
        rules1: {
          name: [{ required: true, message: '必须填写商标名称', trigger: 'blur' }],
          category: [{ required: true, message: '必须选择类别', trigger: 'blur' }],
          register: [{ required: true, message: '必须填写商标注册号', trigger: 'blur' }]
        },
        categoryOpts: {},
        statusOpts: {},
        typeOpts: {},
        regionOpts: {},
        intentionColumns: transactionColumns.intention,
        intentionData: parameters => {
          return getIntentList(Object.assign(parameters, this.queryParam)).then(res => {
            if (res.status === 0) {
              this.categoryOpts = res.result.category_opts
              this.statusOpts = res.result.intentStatusOpts
              this.typeOpts = res.result.tm_type_opts
              this.regionOpts = res.result.tm_region_opts
              return res.result
            }
          })
        },
        searchInput: null,
        statusForm: {},
        itemOpts: {}
      }
    },
    methods: {
      submit () {
        this.Visible = true
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      resetForm () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      onChange (date, dateString) {
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      },
      handleSelect (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleSearch (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleReset (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
      },
      onChangeDate (date, dateString) {
        this.createValue = date
        this.queryParam['filter[created_at_start]'] = dateString[0]
        this.queryParam['filter[created_at_end]'] = dateString[1]
      },
      handleRange (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.$refs.table.refresh(true)
      },
      rangeReset (clearFilters) {
        clearFilters()
        this.createValue = []
        delete this.queryParam['filter[created_at_start]']
        delete this.queryParam['filter[created_at_end]']
        this.$refs.table.refresh(true)
      },
      onChangeDate1 (date, dateString) {
        this.createValue1 = date
        this.queryParam['filter[end_time_start]'] = dateString[0]
        this.queryParam['filter[end_time_end]'] = dateString[1]
      },
      rangeReset1 (clearFilters) {
        clearFilters()
        this.createValue1 = []
        delete this.queryParam['filter[end_time_start]']
        delete this.queryParam['filter[end_time_end]']
        this.$refs.table.refresh(true)
      },
      onEdit (id) {
        const app = this
        app.statusForm = {}
        app.statusForm['id'] = id
        getEditIntention(id).then(res => {
          if (res.status === 0) {
            app.itemOpts = res.result.status_items
            app.visible = true
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      },
      onSubmit () {
        const app = this
        const data = {}
        data['intent[status]'] = app.statusForm.status
        data['intent[notes]'] = app.statusForm.notes
        postEditIntention(app.statusForm['id'], data).then(res => {
          if (res.status === 0) {
            app.visible = false
            app.$refs.table.refresh()
          } else {
            app.$warning({
              title: res.msg
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
